package com.qianf.qfoffice.controller;


import com.github.pagehelper.Page;
import com.qianf.qfoffice.entity.ResponseData;
import com.qianf.qfoffice.entity.User;
import com.qianf.qfoffice.entity.Vip;
import com.qianf.qfoffice.service.VipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class VipController {


    @Autowired
    private VipService vipService;


    @RequestMapping("/vip/add.do")
    @ResponseBody
    public ResponseData insert (HttpSession session, String content) {

        ResponseData<Object> responseData = new ResponseData<>();

        User user = (User) session.getAttribute("user");

        Vip vip = new Vip();
        vip.setContent(content);
        vip.setUno(user.getNo());

        int i = vipService.addVip(vip);

        if (i > 0 ) {

            responseData.setMsg("添加成功");
            return responseData;
        } else {

            responseData.setMsg("添加失败");
            return responseData;
        }


    }


    @RequestMapping("/vip/queryVip.do")
    @ResponseBody
    public Map<String,Object> queryVip (Integer page,Integer limit) {

        HashMap<String, Object> hashMap = new HashMap<>();

        List<Vip> allVip = vipService.findAllVip(page, limit);

        long total = ((Page)allVip).getTotal();

        hashMap.put("code",0);
        hashMap.put("msg","");
        hashMap.put("count",total);
        hashMap.put("data",allVip);

        return hashMap;

    }

}
